package bat;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import javax.per.toolkit.file.FileUtil;

import tools.Env;

public class packbat {
	
	public static void bat(){
		Class<?> cla=packbat.class;
		String batName="packbat";
		String parm="";
		String batPath=Env.get("batPath")+File.separator+batName+".bat";
		try {
			BufferedWriter bw=FileUtil.getWriter(batPath);
			bw.write("@echo off\n");
			bw.write("call initjava.bat\n");
			bw.write("java "+cla.getName()+" "+parm+" \n");
			bw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) throws InterruptedException{
		File[] fs=new File(packbat.class.getResource("packbat.class").getFile()).getParentFile().listFiles();
		String packageName=packbat.class.getPackage().getName();
		for(File f:fs){
			Thread.sleep(1000);
			
			if(f.getName().toUpperCase().equals("packbat.class".toUpperCase())){
				bat();
				continue;
			}
			if(!f.getName().toUpperCase().endsWith(".CLASS"))
				continue;
			String className=packageName+"."+f.getName().substring(0,f.getName().length()-".CLASS".length());
			try {
				Class<?> cls=Class.forName(className);
				Object obj=cls.newInstance();
				Method method=cls.getDeclaredMethod("main", String[].class);
				method.invoke(obj, (Object)(new String[]{}));
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InstantiationException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				
				e.printStackTrace();
			}
		}
	}
	
}
